package com.guoyh.baseutils.utils

import com.google.gson.Gson
import com.guoyh.baseutils.ext.toJson
import com.tencent.mmkv.MMKV
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import java.util.*


/**
 * 应用模块: storage
 * 类描述: 腾讯MMKV序列化存储工具类
 * @author darryrzhoong
 * @since 2020-01-27
 */
class MmkvHelper private constructor() {
    companion object {
        private lateinit var mmkv: MMKV
        val instance: MmkvHelper
            get() = MmkvHolder.INSTANCE
    }

    init {
        mmkv = MMKV.defaultMMKV()
    }

    private object MmkvHolder {
        val INSTANCE = MmkvHelper()
    }

    /**
     * 存入map集合
     *
     * @param key 标识
     * @param map 数据集合
     */
    fun saveInfo(
        key: String,
        map: Map<String, Any>
    ) {
        val gson = Gson()
        val mJsonArray = JSONArray()
        var `object`: JSONObject? = null
        try {
            `object` = JSONObject(gson.toJson(map))
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        mJsonArray.put(`object`)
        mmkv.encode(key, mJsonArray.toString())
    }

    /**
     * 获取map集合
     *
     * @param key 标识
     */
    fun getInfo(key: String?): Map<String, String> {
        val itemMap: MutableMap<String, String> =
            HashMap()
        val result = mmkv.decodeString(key, "")
        try {
            val array = JSONArray(result)
            for (i in 0 until array.length()) {
                val itemObject = array.getJSONObject(i)
                val names = itemObject.names()
                if (names != null) {
                    for (j in 0 until names.length()) {
                        val name = names.getString(j)
                        val value = itemObject.getString(name)
                        itemMap[name] = value
                    }
                }
            }
        } catch (e: JSONException) {
        }
        return itemMap
    }

    /**
     * 扩展MMKV类使其支持对象存储
     */
    fun <T> getResult(key: String, t: Class<T>): T? {
        val str = get(key, "")
        return if (str.isNullOrEmpty()) {
            null
        } else {
            GsonUtil.json2Result(t, str)
        }
    }

    fun putResult(key: String, any: Any?) {
        put(key, any.toJson())
    }


    fun put(key: String, value: String) {
        mmkv.encode(key, value)
    }

    fun put(key: String, value: Int) {
        mmkv.encode(key, value)
    }


    fun put(key: String, value: Long) {
        mmkv.encode(key, value)
    }

    fun put(key: String, value: Boolean) {
        mmkv.encode(key, value)
    }

    fun get(key: String): String {
        val str = mmkv.decodeString(key, "")
        return str ?: ""
    }

    fun getBoolean(key: String, def: Boolean = false): Boolean {
        return mmkv.decodeBool(key, def)
    }

    fun get(key: String, value: String): String {
        val str = mmkv.decodeString(key, "")

        return str ?: ""
    }


    fun getInt(key: String): Int {
        return mmkv.decodeInt(key, 0)
    }

    fun getInt(key: String, value: Int): Int {
        return mmkv.decodeInt(key, value)
    }


    fun getLong(key: String): Long {
        return getLong(key, 0L);
    }

    fun getLong(key: String, value: Long): Long {
        return mmkv.decodeLong(key, value)
    }

    fun clear(key: String) {
        mmkv.removeValueForKey(key)
    }

}


